package runnables;

import log.Logger;
import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import screens.Controller;
import strings.WebcampusResource;
import test.GradeAnalyzer;

public class SuscribirPushRunnable implements Runnable, WebcampusResource{

	// RESOURCES
	private static ResourceBundle resources = ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);

	private boolean isLogin;

	public SuscribirPushRunnable(boolean isLogin){
		this.isLogin=isLogin;
	}

	public void run() {

		GradeAnalyzer ga = GradeAnalyzer.getInstance();
		try{
			// hash , pin
			Logger.log(Integer.toHexString(DeviceInfo.getDeviceId()).toUpperCase()+" is PIN");
			ga.suscribirPush(Controller.getInstance().getHash(),Integer.toHexString(DeviceInfo.getDeviceId()).toUpperCase());
			//System.out.println(Integer.toHexString(DeviceInfo.getDeviceId()).toUpperCase()+" ************* pin ");
		} catch (Exception e){
			Logger.log("PUSH SUSC ERROR");
			System.out.println("********************** push error");
		}

		if (!this.isLogin){
			//if everything is ok, do:
			UiApplication.getUiApplication().invokeLater(new Runnable() {
				public void run() {
					if (Controller.getInstance().getPushStatus()){
						Logger.log("Menu PUSH SUSC OK");
						//TODO test
						UiApplication.getUiApplication().getActiveScreen().invalidate();
						Dialog.inform(resources.getString(ALERT_PUSH_SUSC_OK));
					}
					else { // false
						Logger.log("Menu PUSH SUSC NOT OK");
						Dialog.inform(resources.getString(ALERT_PUSH_SUSC_FAIL));	
					}
				}
			});
		}
		Logger.log("Login PUSH SUSC OK");

	}

}
